#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

static int network_listen(int port)
{
	int fd, flag = 1;
	struct sockaddr_in addr;
	struct linger ling;
	if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket");
		return -1;
	}

	memset(&addr, 0x0, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port   = htons(port);
	addr.sin_addr.s_addr = htons(INADDR_ANY);
	
	if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) == -1)
	{
		perror("setsockopt");
		return -1;		
	}

	memset(&ling, 0x0, sizeof(ling));
	ling.l_onoff  = 1;
	ling.l_linger = 30;
	if(setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)) == -1)
	{
		perror("setsockopt");
		return -1;		
	}

	if(bind(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1)
	{
		perror("bind");
		return -1;
	}

	if(listen(fd, 10) == -1)
	{
		perror("listen");
		return -1;
	}
	
	return fd;
}

int main()
{
	int sfd = network_listen(1107);
	int fd;

        struct sockaddr_in addr;
        socklen_t addr_len = sizeof(struct sockaddr);	
	fd = accept(servfd, (struct sockaddr*)&addr, &addr_len);


}


